package com.charonmc.listeners;

import com.charonmc.CharonHub;
import com.charonmc.utils.PrefixUtils;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.player.*;

public class PlayerListener implements Listener {
    @EventHandler
    public void onPlaceBlock(BlockPlaceEvent event){
        if (!event.getPlayer().hasPermission("Hub.PlaceBlock")){
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onBreakBlock(BlockBreakEvent event){
        if (!event.getPlayer().hasPermission("Hub.BreakBlock")){
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onDropItem(PlayerDropItemEvent event){
        if (!event.getPlayer().hasPermission("Hub.DropItem")){
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onPickItem(PlayerPickupItemEvent event){
        if (!event.getPlayer().hasPermission("Hub.PickItem")){
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onBedEnter(PlayerBedEnterEvent event){
        if (!event.getPlayer().hasPermission("Hub.Bed")){
            event.setCancelled(true);
        }
    }

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event){
        if (event.getTo().getY() < 0){
            event.getPlayer().teleport(Bukkit.getWorlds().get(0).getSpawnLocation());
        }
    }

    @EventHandler
    public void onPlayerHunger(FoodLevelChangeEvent event){
        event.setFoodLevel(20);
        event.setCancelled(true);
    }

    @EventHandler
    public void onPlayerJoinServerGiveDefaultItem(PlayerJoinEvent event){
        CharonHub.setPlayerDefaultInventory(event.getPlayer());
        for (Player p : Bukkit.getOnlinePlayers()){
            if (p.equals(event.getPlayer())){
                continue;
            } else {
                p.hidePlayer(event.getPlayer());
                p.showPlayer(event.getPlayer());
            }
        }
    }

    @EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent event){
        event.setFormat(PrefixUtils.getPlayerPrefix(event.getPlayer()) + event.getPlayer().getDisplayName() + "§7:§f" +event.getMessage());
    }
}
